interface UserInfo {
  id: string;
  name: string;
  avatar: string;
  token: string;
}

interface SystemInfo {
  data: {
    title: string;
    advance_voice_power: number;
  }
}

// 模拟用户信息
const mockUser: UserInfo = {
  id: 'user-123',
  name: '测试用户',
  avatar: 'https://via.placeholder.com/40',
  token: 'mock-token-123',
};

// 模拟系统信息
const mockSystemInfo: SystemInfo = {
  data: {
    title: 'AI智能对话平台',
    advance_voice_power: 10,
  }
};

// 客户端ID
let clientId = '';

/**
 * 获取客户端ID
 * @returns 客户端ID
 */
export function getClientId(): string {
  if (typeof window === 'undefined') {
    return '';
  }
  if (!clientId) {
    // 从本地存储获取或生成新的客户端ID
    clientId = localStorage.getItem('client_id') || generateClientId();
    localStorage.setItem('client_id', clientId);
  }
  return clientId;
}

/**
 * 生成客户端ID
 * @returns 新的客户端ID
 */
function generateClientId(): string {
  return 'client-' + Math.random().toString(36).substring(2, 15);
}

/**
 * 检查会话状态
 * @returns Promise，解析为用户信息或拒绝
 */
export function checkSession(): Promise<UserInfo> {
  return new Promise((resolve, reject) => {
    if (typeof window === 'undefined') {
      return reject(new Error('Cannot check session on the server.'));
    }
    // 检查本地存储中是否有用户信息
    const userInfo = localStorage.getItem('user_info');
    if (userInfo) {
      try {
        resolve(JSON.parse(userInfo));
      } catch (_) {
        reject(new Error('无效的用户信息'));
      }
    } else {
      // 模拟登录状态
      setTimeout(() => {
        localStorage.setItem('user_info', JSON.stringify(mockUser));
        resolve(mockUser);
      }, 100);
    }
  });
}

/**
 * 获取系统信息
 * @returns Promise，解析为系统信息
 */
export function getSystemInfo(): Promise<SystemInfo> {
  return new Promise((resolve) => {
    // 模拟API调用
    setTimeout(() => {
      resolve(mockSystemInfo);
    }, 100);
  });
} 